package com.neuedu.autosale.cli.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.autosale.cli.entity.Resale;
import com.neuedu.autosale.cli.service.ResaleService;
import com.neuedu.autosale.entity.Pager;
import com.neuedu.autosale.entity.Result;

@Controller
public class ResaleController {
	@Autowired
	private ResaleService resaleService;
	
	@RequestMapping("resale/index")
	private String resaleIndex() {
		return "client/resalelist";
	}
	
	@RequestMapping("resale/findall")
	@ResponseBody
	public List<Resale> findAll(){
		return resaleService.findAll();
	}
	
	@RequestMapping("resale/list")
	@ResponseBody
	public Pager<Resale> resaleList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows) {
		Pager<Resale> pager=new Pager<>(page,rows);
		List<Resale> resaleList =resaleService.findListByPager(pager);
		int total=resaleService.findTotalByPager(pager);
		pager.setRows(resaleList);
		pager.setTotal(total);
		return pager;
	}
	
	@RequestMapping("resale/add")
	@ResponseBody
	public Result doAdd(Resale resale) {
		Result result=resaleService.add(resale);
		return result;
	}
	
	@RequestMapping("resale/edit")
	@ResponseBody
	public Result doEdit(Resale resale) {
		Result result=resaleService.edit(resale);
		return result;
	}
	
	@RequestMapping("resale/delete")
	@ResponseBody
	public Result doDelete(Integer resaleId) {
		List<Resale> list=new ArrayList<Resale>();
		list.add(new Resale(resaleId));
		Result result=resaleService.deleteByIds(list);
		return result;
	}
	

}
